package com.xzx.springbootredis.controller;

import com.xzx.springbootredis.entity.Members;
import com.xzx.springbootredis.service.MembersService;
import com.xzx.springbootredis.utils.ImageUtils;
import com.xzx.springbootredis.utils.JsonResult;
import com.xzx.springbootredis.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("api/members")
public class MembersController {
    @Autowired
    private MembersService membersService;

    @PostMapping("register")
    public JsonResult register(@RequestParam("mobile") String mobile, @RequestParam("username") String username) {
        if (!Tool.isMobile(mobile)) {
            return JsonResult.failed("手机号格式无效");
        }
        Members existMember = membersService.getMembersByUsernameOrMobile(mobile);
        if (existMember != null) {
            return JsonResult.failed("手机号已注册");
        }
        Members member = new Members();
        member.setMobile(mobile);
        member.setUsername(username);
        int result = membersService.insertMembers(member);
        return result > 0 ? JsonResult.success("注册成功", null) : JsonResult.failed("注册失败");
    }

    @PostMapping("upload_logo")
    public JsonResult uploadLogo(@RequestParam("id") int id,
                                 @RequestParam("logo") MultipartFile logo) throws IOException {
        // 七牛云上传逻辑
        return JsonResult.success("更新成功", "qiniu_logo_url");
    }
}